<%-- 
    Document   : habitat
    Created on : 30/11/2013, 22:02:56
    Author     : TiagoVieira86
--%>

<%@page import="java.sql.PreparedStatement"%>
<%@page import="package01.Conexão"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="estilo.css" type="text/css"/>
        <title>Cadastro de Habitat - ZooControl</title>
    </head>
    <body>
        <%if(request.getParameter("nome")!=null){
        String nome = request.getParameter("nome");
        session.setAttribute("nome", nome);
        }else if(request.getParameter("sair")!= null){
        session.removeAttribute("nome");
        }
        %>
        <div class="header">
        <h1>Cadastro de Habitat</h1>
        </div>
        <hr/>
        <%if(session.getAttribute("nome")!=null){%>
        <div class="menu">
            <a href="home.jsp">Voltar para a Página Principal</a>
            <hr/>
            <form action="home.jsp" method="post">
            <input type="hidden" name="sair"/>
            <input type="submit" value="Sair"/>
            </form>
        </div>
        <div class="content">
            <form action="" method="get">
                <table>
                    <tr><td>Cod.Habitat: </td><td><input type="text" name="CD_HABITAT"/></td></tr>
                    <tr><td>Nome da Habitat: </td><td><input type="text" name="NM_HABITAT"/></td></tr>
                    <tr><td>Descrição: </td><td><input type="text" name="DS_HABITAT"/></td></tr>
                    <tr><td colspan="3"><input type="submit" value="Adicionar Animal"></td></tr>
                </table>
            </form>
            <hr/>
                    <%
            Connection Con = null;
            Statement stmt = null;
            ResultSet rs = null;
            try{
            Con = Conexão.getConnection("zoocontrol", "zoocontrol");
            if(request.getParameter("CD_HABITAT") != null){
            String insertSQL = "INSERT INTO HABITAT VALUES(?,?,?)";
            PreparedStatement ps = Con.prepareStatement(insertSQL);
            ps.setInt(1, Integer.parseInt(request.getParameter("CD_HABITAT")));
            ps.setString(2, request.getParameter("NM_HABITAT"));
            ps.setString(3, request.getParameter("NM_HABITAT"));
            ps.execute();
            ps.close();
            }else if(request.getParameter("excluirHabitat") != null){
            String insertSQL = "DELETE FROM HABITAT WHERE CD_HABITAT = ?";
            PreparedStatement ps = Con.prepareStatement(insertSQL);
            ps.setInt(1, Integer.parseInt(request.getParameter("excluirHabitat")));
            ps.execute();
            ps.close();
            }
        %>
        <table border="1">
            <tr>
                <th>-</th>
                <th>Cod.Habitat</th>
                <th>Nome do Habitat</th>
                <th>Descrição</th>
            </tr>
            <%
            stmt = Con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM HABITAT");
            while(rs.next()){
            String cd_habitat = rs.getString("CD_HABITAT");
            out.println("<tr>");
            out.println("<td><a href='?excluirHabitat=" + cd_habitat + "'>Excluir</a></td>");
            out.println("<td>" + cd_habitat + "</td>");
            out.println("<td>"+rs.getString("NM_HABITAT")+"</td>");
            out.println("<td>"+rs.getString("DS_HABITAT")+"</td>");
            out.println("</tr>");
            }
            }catch(Exception ex){
            out.println("<tr><td colspan='4'>ERRO: "+ ex.getMessage() +"</td></tr>");
            }finally{
            try{rs.close();}catch(Exception e){}
            try{stmt.close();}catch(Exception e){}
            try{Con.close();}catch(Exception e){}
            }
            %>
        </table>

        </div>
        <%}else{%>
        <h3>Usuário não autorizado</h3>
        <h3><a href="home.jsp">Faça login para acessar.</a></h3>
        <%}%>
    </body>
</html>
